/**
 * 
 */
package com.zit.presentation.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.struts2.json.JSONException;
import org.apache.struts2.json.JSONUtil;

import com.zit.entity.Bedroom;
import com.zit.entity.Berth;
import com.zit.entity.Classes;
import com.zit.entity.Dorm;
import com.zit.entity.Profession;
import com.zit.entity.Student;
import com.zit.entity.Teacher;
import com.zit.entity.Term;
import com.zit.service.BaseService;
import com.zit.service.StudentService;

/**
 * @author 王冲
 * @datetime 2013-12-3
 */
public class StudentAction extends BaseAction {
	
	//所有学生集合
	private List<Student> students=new ArrayList<Student>();
	
	//实例化studentService
	private StudentService studentService=new StudentService();
	
	//学期编号  在deleteStudent中代表学生编号
	private int tid;
	
	//专业编号
	private int pid;
	
	//返回对应班级json字符串
	private String jsonClass;
	
	//添加的学生对象
	private Student student;
	
	//模糊查询学生对象
	public 	Student selectStudent=null;
	/**
	 * 作者: 王冲
	 * 功能：查询所有学生
	 * @return
	 */
	public String show(){
		//查询所有学生
		students=studentService.select(selectStudent);
		//查询所有学期
		requestMap.put("terms", studentService.findAll(Term.class));
		//查询所有专业
		requestMap.put("professions", studentService.findAll(Profession.class));
		//查询所有公寓
		requestMap.put("dorms", studentService.findAll(Dorm.class));
		return "show";
	}
	
	/**
	 * 作者：王冲
	 * 功能：ajax查询对应学期对应专业的班级
	 * @return
	 * @throws JSONException 
	 */
	public String selectClasses() throws JSONException{
		//查询对应学期对应专业的班级  并将转换成json字符串
		jsonClass = JSONUtil.serialize(studentService.select(tid, pid));
		return "selectClasses";
	}
	
	/**
	 * 作者:王冲
	 * 功能:添加学生信息
	 * @return
	 */
	public String addStudent(){
		//添加学生
		studentService.add(student);
		return "addStudent";
	}
	
	/**
	 * 作者：王冲
	 * 功能：删除学生
	 * @return
	 */
	public String deleteStudent(){
		//根据Id查询学生对象
		student=(Student)studentService.selectAStudent(new Student(tid));
		System.out.println(student.getName());
		//将床位设为0代表无人住
		student.getBerth().setRemark("0");
		//删除学生
		studentService.deleteStudent(student);
		return "deleteStudent";
	}
	
	public List<Student> getStudents() {
		return students;
	}

	public void setStudents(List<Student> students) {
		this.students = students;
	}
	
	public int getTid() {
		return tid;
	}

	public void setTid(int tid) {
		this.tid = tid;
	}

	public int getPid() {
		return pid;
	}

	public void setPid(int pid) {
		this.pid = pid;
	}

	public String getJsonClass() {
		return jsonClass;
	}

	public void setJsonClass(String jsonClass) {
		this.jsonClass = jsonClass;
	}
	

	public Student getStudent() {
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}

	public Student getSelectStudent() {
		return selectStudent;
	}

	public void setSelectStudent(Student selectStudent) {
		this.selectStudent = selectStudent;
	}

	public static void main(String[] args) {
		BaseService baseService=new BaseService();
//		
//		Set<Student> students=new HashSet<Student>();
//		Classes c=new Classes(new Profession(1), new Teacher(4), new Teacher(2), new Term(1), "ZIES1210", new Date(), 0, null, students, null, null);
//		Student student1=new Student(new Berth(1), c, "张垚", new Date(), 0, null, 0, 20, "15696121415", "重庆市南川区", 0, null, null, null);
//		Student student2=new Student(new Berth(2), c, "王冲", new Date(), 0, null, 0, 20, "13640542016", "重庆市云阳县", 0, null, null, null);
//		Student student3=new Student(new Berth(3), c, "崔大红", new Date(), 0, null, 0, 20, "15215015814", "云南省昭通市", 0, null, null, null);
//		Student student4=new Student(new Berth(3), c, "崔全文", new Date(), 0, null, 0, 20, "15215015814", "云南省昭通市", 0, null, null, null);
//		Student student5=new Student(new Berth(4), c, "韩超", new Date(), 0, null, 0, 20, "18717011703", "重庆市涪陵区", 0, null, null, null);
//		students.add(student1);
//		students.add(student2);
//		students.add(student3);
//		students.add(student4);
//		students.add(student5);
//		Set<Student> students1=new HashSet<Student>();
//		Classes c1=new Classes(new Profession(1), new Teacher(5), new Teacher(3), new Term(1), "ZIES1209", new Date(), 0, null, students1, null, null);
//		Student student6=new Student(new Berth(5), c1, "孟川西", new Date(), 0, null, 0, 20, "13245655434", "重庆市涪陵区", 0, null, null, null);
//		Student student7=new Student(new Berth(6), c1, "邓伟", new Date(), 0, null, 0, 20, "18634534554", "重庆市大足县", 0, null, null, null);
//		Student student8=new Student(new Berth(7), c1, "王玮", new Date(), 0, null, 0, 20, "1345656798", "重庆市涪陵区", 0, null, null, null);
//		Student student9=new Student(new Berth(8), c1, "梁松松", new Date(), 0, null, 0, 20, "1311312345", "重庆市江津区", 0, null, null, null);
//		students1.add(student6);
//		students1.add(student7);
//		students1.add(student8);
//		students1.add(student9);
//		baseService.save(c);
//		baseService.save(c1);
		
		
		//		Set<Bedroom> bs=new HashSet<Bedroom>();
//		Dorm d=new Dorm(0, "重庆市沙坪坝三联大厦劳动路41号", "一单元", new Date(), 0, null, bs);
//		Set<Berth> brs=new HashSet<Berth>();
//		Bedroom b1=new Bedroom(d, "3-1", new Date(), 0, null, 8, brs);
//		Bedroom b2=new Bedroom(d, "3-2", new Date(), 0, null, 8, null);
//		Bedroom b3=new Bedroom(d, "3-3", new Date(), 0, null, 8, null);
//		Bedroom b4=new Bedroom(d, "3-4", new Date(), 0, null, 8, null);
//		Bedroom b5=new Bedroom(d, "3-5", new Date(), 0, null, 8, null);
//		Bedroom b6=new Bedroom(d, "3-6", new Date(), 0, null, 8, null);
//		
//		Berth br1=new Berth(b1, "1", new Date(), 0, null, null);
//		Berth br2=new Berth(b1, "2", new Date(), 0, null, null);
//		Berth br3=new Berth(b1, "3", new Date(), 0, null, null);
//		Berth br4=new Berth(b1, "4", new Date(), 0, null, null);
//		Berth br5=new Berth(b1, "5", new Date(), 0, null, null);
//		Berth br6=new Berth(b1, "6", new Date(), 0, null, null);
//		Berth br7=new Berth(b1, "7", new Date(), 0, null, null);
//		Berth br8=new Berth(b1, "8", new Date(), 0, null, null);
//		
//		brs.add(br1);
//		brs.add(br2);
//		brs.add(br3);
//		brs.add(br4);
//		brs.add(br5);
//		brs.add(br6);
//		brs.add(br7);
//		brs.add(br8);
//		
//		
//		bs.add(b1);
//		bs.add(b2);
//		bs.add(b3);
//		bs.add(b4);
//		bs.add(b5);
//		bs.add(b6);
//		baseService.save(d);
	}
}
